<?php
// +----------------------------------------------------------------------
// | IPHP
// +----------------------------------------------------------------------
// | This is an open source and simple PHP FrameWork.
// +----------------------------------------------------------------------
// | Author: wuzhuo
// +----------------------------------------------------------------------

// sae的存储

namespace IPHP\Storage;

class Sae {
    public $mc;

    // 构造方法
    public function __construct() {
        if(!function_exists('memcache_init')) {
            header("Content-Type:text/html;charset=utf-8");
            exit('此代码仅能运行在SAE平台上');
        }
        $this->mc = memcache_init();

        if(!$this->mc) {
            header("Content-Type:text/html;charset=utf-8");
            exit('需要先初始化memcache服务才能使用');
        }
    }

    /**
     * 设置
     */
    public function set($dir,$filename,$content){
        $this->mc->set($filename,$content,MEMCACHE_COMPRESSED,0);
    }

    /**
     * 获取
     * @return [type] [description]
     */
    public function get($filename){
        $filename = basename($filename);
        if(!$filename) return false;
        return $this->mc->get($filename);
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete($filename) {
        $this->mc->delete($filename);
    }

    /**
     * 检查文件是否存在
     * @return [type] [description]
     */
    public function exists($filename) {        
        $content = $this->get($filename);
        return $content ? true : false;
    }

    /**
     * 获取文件信息
     * @return [type] [description]
     */
    public function info($filename) {
        $content = $this->get($filename);
        $info['mtime'] = substr($content, 0, 10);
        return $info;
    }
}